package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Customer;


@Component
public class CustomerDAO {

	 @PersistenceContext
	  private EntityManager em;
	/**
	 * @return all accounts from Account table
	 */
	 @Transactional
	public List<Customer> getCustomers() {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = em.createQuery("SELECT a FROM Customer a", Customer.class);
		List<Customer> customers = null;
		customers = query.getResultList();
		return customers;
	}
	
	/**
	 * Add account to Account table
	 * @param account Account entity that contains mapped with each column in Account table
	 * @return
	 */
	@Transactional
	public boolean addCustomer(Customer customer) {
//		EntityManager em = TransactionManager.getEntityManager();
		em.persist(customer);
		return true;
	}
	/**
	 * return a customer by loginId
	 * @param customerId
	 * @return
	 */
	@Transactional
	public Customer getCustomer(String loginId) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = em.createQuery("SELECT c FROM Customer c WHERE c.loginId = :id", Customer.class);
		Customer customer = query.setParameter("id", loginId).getSingleResult();
		return customer;
	}
	/**
	 * Check when logging
	 * @param id
	 * @param pass
	 * @return
	 */
	@Transactional
	public Integer checkLogin(String id, String pass) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Customer> query = em.createQuery("SELECT c FROM Customer c WHERE c.loginId = :id AND c.password = :pass", Customer.class);
		return query.setParameter("id", id).setParameter("pass", pass).getResultList().size();
	}
	/**
	 * update customer
	 * @param cus
	 * @return
	 */
	@Transactional
	public boolean updateCustomer(Customer cus) {
		String temp = "UPDATE " + Customer.class.getName()
				+ " SET password = :password, "
				+ " firstName = :firstName , lastName = :lastName, "
				+ " address= :address , phone = :phone " + "WHERE id = :id";
//		EntityManager em = TransactionManager.getEntityManager();
		Query query = em.createQuery(temp);
		query.setParameter("id", cus.getId());
		query.setParameter("password", cus.getPassword());
		query.setParameter("firstName", cus.getFirstName());
		query.setParameter("lastName", cus.getLastName());
		query.setParameter("address", cus.getAddress());
		query.setParameter("phone", cus.getPhone());
		return query.executeUpdate() > 0;
	}
	/**
	 * Update password when user change it.
	 * @param password
	 * @param id
	 * @return
	 */
	@Transactional
	public boolean updatePassword(String password, Long id) {
		String temp = "UPDATE " + Customer.class.getName()
				+ " SET password = :password WHERE id = :id";
//		EntityManager em = TransactionManager.getEntityManager();
		Query query = em.createQuery(temp);
		query.setParameter("id", id);
		query.setParameter("password", password);
		return query.executeUpdate() > 0;
	}
}
